﻿using System;

namespace Odin.Contracts.Data
{
	public enum SessionStatus
	{
		Idle,
		Running,
		Completed,
		NoSession
	}

	public delegate void MethodChangedEventHandler(Session session, Method method);

	public class Session
	{
		public const string SessionIdentifierKey = "SessionId";
		public event MethodChangedEventHandler MethodChanged = (s, e) => { };

		public Session()
		{
			if( null == Methods )
			{
				Methods = new MethodCollection();	
			}
			
			Status = SessionStatus.Idle;
		}

		public string Identifier { get; set; }

		public string XapPath { get; set; }
		public string StartupObject { get; set; }

		public MethodCollection Methods { get; private set; }
		public SessionStatus Status { get; set; }

		
		public void OnMethodChanged(Method method)
		{
			var found = false;
			foreach( var existingMethod in Methods )
			{
				if( method.Namespace.Equals(existingMethod.Namespace) &&
					method.Class.Equals(existingMethod.Class) &&
					method.MethodName.Equals(existingMethod.MethodName))
				{
					existingMethod.Result = method.Result;
					existingMethod.StackTrace = method.StackTrace;
					existingMethod.Message = method.MethodName;
					existingMethod.LastModified = method.LastModified;
					found = true;
					break;
				}
			}
			if( !found )
			{
				Methods.Add(method);
			}
			MethodChanged(this, method);
		}
	}
}
